package com.warwithinme.android.widgetwar;

import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.provider.Settings;

public class ButtonAirplane extends TButton
{

	private final static String INTENT_ACTION = "com.warwithinme.android.widgetwar.ButtonAirplane";
	private Intent _intent;

	public int getButtonBmp(Context context)
	{
		if (_intent != null)
		{
			int state = _intent.getIntExtra("state", 0);
			_intent = null;
			if (state == 0)
				return R.drawable.airplane_on;
			else
				return R.drawable.airplane_off;
		}

		// If this method is call when not receiving a intent from android
		// system,
		// Then invoke this block.
		int airplaneMode = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0);
		if (airplaneMode == 1)
		{
//			Log.v("armNclover", "airplaneMode is enabled.");
			return R.drawable.airplane_on;
		} else
		{
//			Log.v("armNclover", "airplaneMode is disabled.");
			return R.drawable.airplane_off;
		}
	}

	@Override
	public void toggleSetting(Context context) {
		
		ContentResolver cr = context.getContentResolver();
//		Log.v("armNclover", Settings.System.getString(cr, Settings.System.AIRPLANE_MODE_RADIOS));
		int isEnabled = Settings.System.getInt(cr, Settings.System.AIRPLANE_MODE_ON,0);
		if(isEnabled == 0)
		{
			Settings.System.putString(cr, Settings.System.AIRPLANE_MODE_RADIOS, getAirplaneModeRadios(context));
			Settings.System.putInt(cr,Settings.System.AIRPLANE_MODE_ON, 1);
		}else{
			Settings.System.putString(cr, Settings.System.AIRPLANE_MODE_RADIOS, "cell,bluetooth,wifi");
			Settings.System.putInt(cr,Settings.System.AIRPLANE_MODE_ON, 0);
		}

		// Post an intent to reload
		Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
		intent.putExtra("state", isEnabled);
		PendingIntent pIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
		try
        {
	        pIntent.send();
        } catch (CanceledException e)
        {
	        e.printStackTrace();
        }
	}

	@Override
	public String getIntentAction()
	{
		return INTENT_ACTION;
	}

	@Override
	public int canHandleIntent(Intent intent)
	{
		if (intent.getAction().equals(Intent.ACTION_AIRPLANE_MODE_CHANGED))
		{
			_intent = intent;
			return ToggleWidget.HANDLING_TYPE_DONE;
		} else
		{
			return ToggleWidget.HANDLING_TYPE_UNABLE;
		}
	}

	private String getAirplaneModeRadios(Context context)
	{
		String mode = "cell";
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
		if(sp.getBoolean("AirPlaneMode_BT_OFF",true))
			mode += ",bluetooth";// mode + "," + Settings.System.RADIO_BLUETOOTH;
		if(sp.getBoolean("AirPlaneMode_WF_OFF", true))
			mode += ",wifi";//mode + "," + Settings.System.RADIO_WIFI;
		return mode;
	}
}
